package shop.xmall.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shop.xmall.system.domain.SysUserRole;
import shop.xmall.system.mapper.SysUserMapper;
import shop.xmall.system.domain.SysUser;
import shop.xmall.system.mapper.SysUserRoleMapper;
import shop.xmall.system.service.ISysUserService;
import shop.xmall.utils.DateUtils;
import shop.xmall.utils.StringUtils;
import shop.xmall.utils.text.Convert;

/**
 * 用户信息Service业务层处理
 *
 * @author xmall
 * @date 2019-11-14
 */
@Service
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询用户信息
     *
     * @param userId 用户信息ID
     * @return 用户信息
     */
    @Override
    public SysUser selectSysUserById(Long userId) {
        return sysUserMapper.selectSysUserById(userId);
    }

    /**
     * 查询用户信息列表
     *
     * @param sysUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser) {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增用户信息
     *
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    public int insertSysUser(SysUser sysUser) {
        sysUser.setCreateTime(DateUtils.getNowDate());
        int result = sysUserMapper.insertSysUser(sysUser);
        if (result > 0 && StringUtils.isNotBlank(sysUser.getRoleIds())) {
            List<SysUserRole> sysUserRoleList = new ArrayList<>();
            Long[] roleIds = Convert.toLongArray(sysUser.getRoleIds());
            SysUserRole ur;
            for (Long roleId : roleIds) {
                ur = new SysUserRole();
                ur.setUserId(sysUser.getUserId());
                ur.setRoleId(roleId);
                sysUserRoleList.add(ur);
            }
            sysUserRoleMapper.batchUserRole(sysUserRoleList);
        }
        return result;
    }

    /**
     * 修改用户信息
     *
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUser sysUser) {
        sysUser.setUpdateTime(DateUtils.getNowDate());
        int result = sysUserMapper.updateSysUser(sysUser);
        if (result > 0) {
            //刪除角色信息
            sysUserRoleMapper.deleteUserRoleByUserId(sysUser.getUserId());
            List<SysUserRole> sysUserRoleList = new ArrayList<>();
            Long[] roleIds = Convert.toLongArray(sysUser.getRoleIds());
            SysUserRole ur;
            for (Long roleId : roleIds) {
                ur = new SysUserRole();
                ur.setUserId(sysUser.getUserId());
                ur.setRoleId(roleId);
                sysUserRoleList.add(ur);
            }
            sysUserRoleMapper.batchUserRole(sysUserRoleList);
        }
        return result;
    }

    /**
     * 删除用户信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysUserByIds(String ids) {
        int result = sysUserMapper.deleteSysUserByIds(Convert.toStrArray(ids));
        if (result > 0) {
            //刪除角色信息
            Long[] userIds = Convert.toLongArray(ids);
            for (Long userId : userIds) {
                //刪除角色信息
                sysUserRoleMapper.deleteUserRoleByUserId(userId);
            }
        }
        return result;
    }

    /**
     * 删除用户信息信息
     *
     * @param userId 用户信息ID
     * @return 结果
     */
    @Override
    public int deleteSysUserById(Long userId) {
        int result = sysUserMapper.deleteSysUserById(userId);
        if (result > 0) {
            //刪除角色信息
            sysUserRoleMapper.deleteUserRoleByUserId(userId);
        }
        return result;
    }

    @Override
    public int isExist(SysUser sysUser) {
        int count = sysUserMapper.isExist(sysUser);
        return count;
    }
}
